package com.jhz.speedy.common.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.jhz.speedy.component.application.MyApplication;

/**
 * 轻量级储存
 * Created by JunHeZ on 2017/6/28.
 */
public class SharedUtils {
    // 文件名
    private static final String FILE_NAME = MyApplication.getInstance().getPackageInfo().packageName;
    private static SharedUtils sharedUtils;
    private SharedPreferences sharedPreferences;

    private SharedUtils() {
        sharedPreferences = MyApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }

    public static SharedUtils getInstance() {
        if (sharedUtils == null) {
            sharedUtils = new SharedUtils();
        }
        return sharedUtils;
    }

    public String get(String key) {
        return sharedPreferences.getString(key, null);
    }


    public boolean save(String key, String value) {
        return sharedPreferences.edit().putString(key, value).commit();
    }

    public boolean getBoolean(String key){
        return sharedPreferences.getBoolean(key, false);
    }

    public boolean saveBoolean(String key, boolean value){
        return sharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 清空配置文件保存的信息
     */
    public boolean clearPreferences() {
        return sharedPreferences.edit().clear().commit();
    }

}
